//
//  ViewController.m
//  GetPhotoSize
//
//  Created by mac on 2018/11/27.
//  Copyright © 2018年 杨洋. All rights reserved.
//

#import "ViewController.h"

#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *imageMessage;

@property (nonatomic, strong) NSMutableString *imageInfoString;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
}

-(NSMutableString *)imageInfoString
{
    if (!_imageInfoString) {
        _imageInfoString = [NSMutableString new];
    }
    return _imageInfoString;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];;
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openLibrary];
    }];
    
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openCamera];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:photoAction];
    [alertController addAction:cameraAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - ----------------------打开相册----------------------
-(void)openLibrary
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipc.delegate = self;
    //    [self presentViewController:ipc animated:YES completion:nil];
    [self presentViewController:ipc animated:YES completion:^{
        ipc.delegate = self;
    }];
}
#pragma mark - ----------------------打开相机----------------------
-(void)openCamera
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:nil];
}

-(void)createUI
{
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, kScreenWidth - 20)];
    [self.view addSubview:_imageView];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    _imageView.backgroundColor = [UIColor grayColor];
    
    _imageMessage = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_imageView.frame) + 20, kScreenWidth - 20, kScreenHeight - CGRectGetMaxY(_imageView.frame) - 20 - 20)];
    _imageMessage.backgroundColor = [UIColor grayColor];
    _imageMessage.font = [UIFont systemFontOfSize:20];;
    _imageMessage.numberOfLines = 0;
    
    [self.view addSubview:_imageMessage];
    
}

#pragma mark - ----------------------获取图片的详细信息----------------------
-(void)getImageDetailInfo:(NSURL*)imageUrl{
    __block NSString* imageFileName;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageUrl
                   resultBlock:^(ALAsset *myasset){
                       ALAssetRepresentation *representation = [myasset defaultRepresentation];
                       imageFileName = [representation filename];
                       NSLog(@"图片路径名：%@",imageFileName);
                       NSLog(@"图片UTI：%@",[representation UTI]);
                       NSLog(@"图片URL：%@",[representation url]);
                       
                       NSLog(@"currentThread = %@",[NSThread currentThread]);
                       
                       [self.imageInfoString appendFormat:@"图片路径名：%@\n图片UTI：%@\n图片URL：%@\n",imageFileName,[representation UTI],[representation url]];
                       
                   }
                  failureBlock:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info
{
    
    _imageInfoString = [NSMutableString new];
    
    NSURL *imageAssetUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
    [self getImageDetailInfo:imageAssetUrl];
    
    NSLog(@"info = %@",info);
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _imageView.image = image;
    
    
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.resizeMode = PHImageRequestOptionsResizeModeFast;
    options.networkAccessAllowed = YES;
    
    PHAsset *asset = [info objectForKey:UIImagePickerControllerPHAsset];
    
    [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
        NSUInteger dataLength = imageData.length;
        NSString *bytes = [self getBytesFromDataLength:dataLength];
        NSLog(@"bytes = %@",bytes);
        [self.imageInfoString appendFormat:@"图片大小：%@\n",bytes];
        self.imageMessage.text = self.imageInfoString;
        
    }];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"点击了取消");
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - ----------------------计算图片大小(size)----------------------
- (NSString *)getBytesFromDataLength:(NSInteger)dataLength {
    NSString *bytes;
    if (dataLength >= 0.1 * (1024 * 1024)) {
        bytes = [NSString stringWithFormat:@"%0.1fM",dataLength/1024/1024.0];
    } else if (dataLength >= 1024) {
        bytes = [NSString stringWithFormat:@"%0.0fK",dataLength/1024.0];
    } else {
        bytes = [NSString stringWithFormat:@"%zdB",dataLength];
    }
    return bytes;
}


@end
